package com.ruanjian.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruanjian.reggie.common.R;
import com.ruanjian.reggie.entity.User;
import com.ruanjian.reggie.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
@Slf4j
public class userController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public R<User> login(HttpServletRequest request, @RequestBody User user){
        /**
         * 这里因为没有开通阿里云短信验证使用的是随机生成数代替验证码
         * 所以没有对比验证直接登录或者注册成新用户
         * */
        log.info(request.getSession().toString());
        User userOne = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getPhone, user.getPhone()));
        if (userOne == null){
            userOne = new User();
            userOne.setPhone(user.getPhone());
            userOne.setStatus(1);
            userService.save(userOne);
            request.getSession().setAttribute("user",userOne.getId());
            return R.success(userOne);
        }
        else {
            request.getSession().setAttribute("user",userOne.getId());
            return R.success(userOne);

        }


    }

    @PostMapping("/loginout")
    public R<String> loginOut(HttpSession session){
        session.setAttribute("user",null);
        return R.success("退出成功");
    }


    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user){
        return null;
    }





}
